/* global window */
import { message } from 'antd'
import pathToRegexp from 'path-to-regexp'
import modelExtend from 'dva-model-extend'
import { config } from 'utils'
import { create, remove, update, query } from 'services/entity.js'
import * as basicData from 'services/basicData.js'
import { detailModel } from '../common'

export default modelExtend(detailModel, {
  namespace: 'entityDetail',

  state: {
    data: {},
    editType: 'create',
    parentId: '',
    countryCodeData: [],
    entityLegalFormData: [],
    hintOptions: [],
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((pathname) => {
        const match = pathToRegexp('/entity/detail/:customerId/:id').exec(pathname.pathname)
        if (match) {
          if (match[2] !== config.ADDFLAG) {
            dispatch({ type: 'query', payload: { id: match[2], parentId: match[1] } })
          }
          dispatch({ type: 'iniBasicData' })
          dispatch({ type: 'updateState', payload: { parentId: match[1] } })
        }
      })
    },
  },

  effects: {
    * query ({ payload = {} }, { call, put }) {
      const data = yield call(query, payload)
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            editType: 'update',
            data: data.data,
          },
        })
      } else {
        message.error('get data failed')
        throw data
      }
    },

    * delete ({ payload }, { call, put, select }) {
      const data = yield call(remove, { id: payload })
      const { selectedRowKeys } = yield select(_ => _.entityDetail)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'query' })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * create ({ payload }, { select, call, put }) {
      const { parentId } = yield select(_ => _.entityDetail)
      payload.customerId = parentId
      const data = yield call(create, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'query', payload: { id: data.data, parentId } })
      } else {
        message.error('create failed')
        throw data
      }
    },

    * update ({ payload }, { select, call, put }) {
      const { parentId } = yield select(_ => _.entityDetail)
      payload.customerId = parentId
      const data = yield call(update, payload)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'query', payload: { id: data.data, parentId } })
      } else {
        message.error('update failed')
        throw data
      }
    },

    * iniBasicData ({ payload = {} }, { call, put }) {
      const countryCode = yield call(basicData.listall, { type: 'CountryCodes' })
      if (countryCode.success) {
        yield put({
          type: 'updateState',
          payload: {
            countryCodeData: countryCode.data,
          },
        })
      }
      const entityLegalForm = yield call(basicData.listall, { type: 'EntityLegalForm' })
      if (entityLegalForm.success) {
        yield put({
          type: 'updateState',
          payload: {
            entityLegalFormData: entityLegalForm.data,
          },
        })
      }
    },

  },

  reducers: {

    updateEditState (state, { payload }) {
      return { ...state, ...payload }
    },
  },
})
